package com.portme.client.pages;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.portme.client.PortmeServiceClient;
import com.portme.client.StaticFactory;
import com.portme.client.pages.richtext.RichTextToolbar;
import com.portme.client.pages.widgets.PostDisplayWidget;
import com.portme.client.pages.widgets.SideBar;
import com.portme.shared.ClientCustomTab;
import com.portme.shared.ClientPost;
import com.portme.shared.LoginInfo;

public class SingleTabWidget extends ScrollPanel{
	
	private ClientCustomTab currentTab;
	private Long selectedPost = null;
	
	//Widgets
	
	private VerticalPanel holdingPanel = new VerticalPanel();
	private HorizontalPanel toolbarPanel = new HorizontalPanel();
	
	private HorizontalPanel contentPanel = new HorizontalPanel();
	private SideBar linksPanel;
	private PostDisplayWidget displayPostWidget = new PostDisplayWidget();
	
	private TextBox postTitleBox = new TextBox();
	private RichTextArea postRichTextBox = new RichTextArea();
	
	//Click handlers
	private ClickHandler publishPostHandler = new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
			if(!postTitleBox.getText().isEmpty()) {
				PortmeServiceClient.getInstance().createPost(currentTab.getId(), postTitleBox.getText(), postRichTextBox.getHTML(), new AsyncCallback<ClientPost>() {
					
					@Override
					public void onSuccess(ClientPost result) {
						if(linksPanel != null) {
							LoginInfo currentUser = StaticFactory.getInstance().getCurrentUserLogin();
							if(currentUser!=null && currentUser.getUsername() != null) {
								linksPanel.addPost(currentUser.getUsername().split("@")[0], result);
							}
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						Window.alert("Could not create post!");
					}
				});
			} else {
				Window.alert("Title must be set!");
			}
		}
	};
	
	private ClickHandler discardPostHandler = new ClickHandler() {
		
		@Override
		public void onClick(ClickEvent event) {
			displayPostWidget.clear();
		}
	};
	
	public SingleTabWidget(String user, ClientCustomTab tab, Long selectedPost) {
		holdingPanel.setWidth("100%");
		contentPanel.setWidth("100%");
		//this.setHeight("550px");
		this.currentTab = tab;
		if(selectedPost != null) {
			this.selectedPost = selectedPost;
		} else {
			this.selectedPost = currentTab.getFirstPost();
		}
		add(holdingPanel);
		holdingPanel.add(toolbarPanel);
		
		displayPostWidget.setCurrentPost(this.selectedPost);
		displayPostWidget.setTabId(this.currentTab.getId());
		linksPanel = new SideBar(currentTab);
		contentPanel.add(linksPanel);
		contentPanel.add(displayPostWidget);
		postTitleBox.setWidth("99%");
		displayPostWidget.setSpacing(10);
		displayPostWidget.addStyleName("postEntryContainer");
		
		contentPanel.setCellWidth(linksPanel, "25%");
		contentPanel.setCellWidth(displayPostWidget, "75%");
		
		linksPanel.setDisplayWidget(displayPostWidget);
		
		holdingPanel.add(contentPanel);
		createWidget(user);
	}

	private void createWidget(final String user) {
		if(this.currentTab != null) {
			Button addPost = new Button("Add Post", new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					displayPostWidget.clear();
					displayPostWidget.add(new Label("Title : "));
					postTitleBox.setText("");
					displayPostWidget.add(postTitleBox);
					displayPostWidget.add(new Label("Body : "));
					
					//RichTextArea postRichTextBox = new RichTextArea();
					postRichTextBox.setHTML("");
				    postRichTextBox.ensureDebugId("cwRichText-area");
				    postRichTextBox.setSize("100%", "14em");
				    RichTextToolbar toolbar = new RichTextToolbar(postRichTextBox);
				    toolbar.ensureDebugId("cwRichText-toolbar");
				    toolbar.setWidth("100%");

				    // Add the components to a panel
				    Grid grid = new Grid(2, 1);
				    grid.setStyleName("cw-RichText");
				    grid.setWidget(0, 0, toolbar);
				    grid.setWidget(1, 0, postRichTextBox);
					
					displayPostWidget.add(grid);
					HorizontalPanel buttons = new HorizontalPanel();
					buttons.setSpacing(5);
					buttons.add(new Button("Publish Post", publishPostHandler));
					buttons.add(new Button("Discard Post", discardPostHandler));
					displayPostWidget.add(buttons);
				}
			});
			
			if(StaticFactory.getInstance().getCurrentUserLogin() != null 
					&& StaticFactory.getInstance().getCurrentUserLogin().getUsername() != null
					&& StaticFactory.getInstance().getCurrentUserLogin().getUsername().split("@")[0].equals(user)) {
				toolbarPanel.add(addPost);
				holdingPanel.setCellHeight(toolbarPanel, "40px");
			}
			else 
				toolbarPanel.setVisible(false);
			if(currentTab.getPosts() != null) {
				/*for(final ClientPost post : currentTab.getPosts()) {
					//TODO: In future check if user has marked some post as "show on entering tab" 
					
					Label postLink = new Label(post.getTitle());
					postLink.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							// TODO Auto-generated method stub
							History.newItem(""+user+"/"+currentTab.getId()+"/"+post.getId(), false);
							
							displayPostWidget.clear();
							displayPostWidget.add(new Label(post.getTitle()));
							HTML html = new HTML(post.getText());
							html.setWordWrap(true);
							displayPostWidget.add(html);
							Window.setTitle(post.getTitle());
						}
					});
					toolbarPanel.add(postLink);
					
					if(selectedPost != null && selectedPost.equals(post.getId())) {
						displayPostWidget.clear();
						displayPostWidget.add(new Label(post.getTitle()));
						HTML html = new HTML(post.getText());
						html.setWordWrap(true);
						displayPostWidget.add(html);
					}
				}*/
				
				linksPanel.createWidget(user);
				if(selectedPost != null ) {
					linksPanel.selectPost(selectedPost);
				} else {
					linksPanel.selectFirst(user);
				}
			}
		}
	}
	
	public Long getTabId() {
		if(currentTab != null) {
			return currentTab.getId();
		}
		return null;
	}
	
}
